<%@ page pageEncoding="UTF-8"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/functions" prefix="fn"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/fmt" prefix="fmt"%>
<%@ taglib uri="http://www.ld.com/shield/tags" prefix="shield"%>
<%@ page trimDirectiveWhitespaces="true" %>
<fmt:setLocale value="zh_CN"/>
<shield:permssion type="button">
<form >
<table  class="tab-list shield-table-odd"  id="popupSelect" >
	<input id="relationIds" type="hidden"   value="" name="relationIds" />
	<input id="userId" type="hidden"    value="${id}" name="userId" />
    <tr>
        <th class="w50 taCenter">序号</th>
	    <th>用户姓名</th>
	    <th>手机号码</th>
	     <th>性别</th>
    </tr>
<c:forEach var="model" items="${pageBean.resultList }" varStatus="var">
    <tr class="userSelect" data-id="${model.id}" data-name="${model.userName}"  >
        <td class="taCenter">${(pageBean.currentPage -1)*pageBean.pageSize+var.count }</td>
	   	<td >${model.userName }</td>
	   	<td >${model.mobile }</td>
	   	<td ><shield:switchOput data="0=女|1=男" value="${model.sex }"></shield:switchOput></td>
    </tr>
</c:forEach>
<c:if test="${empty pageBean.resultList }">
 	<!-- 无数据时展示，有数据时展示上面的列表 -->
	<tr >
        <td colspan="4" class="taCenter">暂无数据</td>
    </tr>
</c:if>

</table>
</form>

</shield:permssion>
<%@include file="/WEB-INF/view/a/include/pagination.jsp"%>
<div class="search-bt taCenter">
	<input class="infoAdd selectBtn" type="button"  value="确定">
</div>	
  <script type="text/javascript">
  var selectParams=":button.selectBtn";
  var relationParams="#relationIds";
  var num =100;//最多选择几个
  $(function(){
	 var initDataId="";
	 var initDataName="";
	 $("#selectUserContent").find('img').each(function(index){
		 if(index==0)
		 {
		 	initDataId=$(this).data("id")
		 	initDataName= $(this).data("name");
		 }else
		 {
			 initDataId=initDataId+","+$(this).data("id")
			 initDataName=initDataName+","+ $(this).data("name");
		 }
	 });
	  $(relationParams).val(initDataId);
	  $(relationParams).data("name",initDataName);
	  $('tr.userSelect').click(function(){
		  var dataId = $(relationParams).val();
		  var dataName = $(relationParams).data("name");
		  //判断是否已经选择过数据
		  console.log(dataId);
		  if(dataId){
		  var tempDataId = ","+dataId+",";
	  	  if(tempDataId.indexOf(","+$(this).data("id")+",")!=-1){
	  	  	return false;
		  }else
		  {
		    var  data = dataId+","+$(this).data("id");
		   	var length = 	data.split(",").length;
		   	if(num!=0&&length>num)
	   		{
		   		ShieldJS.alert("title","最多选择"+num+"个！");
		   		return false;
	   		}
			$(relationParams).val(data);
		  }
		  }else
		  {
			  $(relationParams).val($(this).data("id"));
		  }
		  if(dataName){
			  $(relationParams).data("name",dataName+","+$(this).data("name"));
		  }else{
		  		$(relationParams).data("name",$(this).data("name"));
		  }
		  $("#selectUserContent").append("<tr><td>"+$(this).data("name")+"</td><td style='text-align:center'><a><img src='${ctxPath}/images/icon/delete.png' data-id='"+$(this).data("id")+"' data-name='"+$(this).data("name")+"' onClick='remove(this)' class='c_p' title='删除'></a></td></tr>");
	  });
	  $(selectParams).click(function(){
		  $.ajax({
			  //请求方式
	            type : "POST",
	            //请求地址
	            url : "${ctxPathA}/oa/work/userRelation/saveSupervisor",
	            //数据，json字符串
	            data : {relationIds:$("#relationIds").val(),userId:$("#userId").val(),type:${type}},
	            dataType:'json',
	            //请求成功
	            success : function(result) {
	            	if(result.success)
	            		{
	            		ShieldJS.alert("消息", result.message, "success");
	            		ShieldJS.dialog.remove();
	            		}else
            			{
	            			ShieldJS.alert("消息", result.message, "error");
            			}
	            },
	            //请求失败，包含具体的错误信息
	            error : function(e){
	                console.log(e.status);
	                console.log(e.responseText);
	                ShieldJS.alert("消息", e.responseText, "error");
	            }
		  });
	  });
	  
  })
  function remove(obj)
  {
	  ShieldJS.confirm("温馨提示", "确定要删除吗?删除后请点击‘确定’按钮。", function(){
		  var dataOldId = ","+$(relationParams).val()+",";
		  var dataOldName = ","+$(relationParams).data("name")+",";
		  dataOldId =  dataOldId.replace(","+$(obj).data("id")+",",",");

		  if(dataOldId.length==1)
		  {
			  dataOldId=dataOldId.substring(0,dataOldId.length-1);
		  }else
		  {
			  dataOldId=dataOldId.substring(1,dataOldId.length-1);
		  }
		  dataOldName =   dataOldName.replace(","+$(obj).data('name')+",",",");
		  if(dataOldName.length==1)
		  {
			  dataOldName=dataOldName.substring(0,dataOldName.length-1);
		  }else
		  {
			  dataOldName=dataOldName.substring(1,dataOldName.length-1);
		  }
		  var dataId = $(relationParams).val(dataOldId);
		  var dataName = $(relationParams).data("name",dataOldName);
		 $(obj).parents('tr').remove();
	  });
	 
  }
  
  
  </script>
